<!--
  * chenyuqiang
  *
  * @Author:    chenyuqiang
  * @Date:      2025-10-16 19:03:13
  * @Copyright  1
-->
<template>
  <a-modal
      :title="form.id ? '编辑CMDB' : '添加CMDB'"
      :width="800"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form 
      ref="formRef" 
      :model="form" 
      :rules="rules" 
      :label-col="{ span: 8 }"
      :wrapper-col="{ span: 14 }"
    >
      <a-row :gutter="16">
        <a-col :span="12">
          <a-form-item label="设备名称" name="deviceName">
            <a-input v-model:value="form.deviceName" placeholder="请输入设备名称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="设备类型" name="type">
            <a-select v-model:value="form.type" placeholder="请选择设备类型">
              <a-select-option v-for="item in dictStore.getDictData(DICT_CODE_ENUM.CMDB_TYPE)" :key="Number(item.dataValue)" :value="Number(item.dataValue)">
                {{ item.dataLabel }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>

      <a-row :gutter="16">
        <a-col :span="12">
          <a-form-item label="IP地址" name="ip">
            <a-input v-model:value="form.ip" placeholder="请输入IP地址" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="子网掩码" name="mask">
            <a-input v-model:value="form.mask" placeholder="请输入子网掩码" />
          </a-form-item>
        </a-col>
      </a-row>

      <a-row :gutter="16">
        <a-col :span="12">
          <a-form-item label="安全域名称" name="hostName">
            <a-input v-model:value="form.hostName" placeholder="请输入安全域名称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="所属部门" name="deptName">
            <a-input v-model:value="form.deptName" placeholder="请输入所属部门" />
          </a-form-item>
        </a-col>
      </a-row>

      <a-row :gutter="16">
        <a-col :span="12">
          <a-form-item label="负责人" name="leader">
            <a-input v-model:value="form.leader" placeholder="请输入负责人" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { cmdbApi } from '/@/api/business/cmdb/cmdb-api';
  import { smartSentry } from '/@/lib/smart-sentry';
  import { useDictStore } from '/@/store/modules/system/dict.js';
  import { DICT_CODE_ENUM } from '/@/constants/support/dict-const.js';

  // ------------------------ 字典数据 ------------------------
  const dictStore = useDictStore();

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
      deviceName: undefined, //设备名称
      ip: undefined, //ip
      mask: undefined, //掩码
      leader: undefined, //负责人
      type: undefined, //1 设备数据 2 安全域数据
      hostName: undefined, //安全域名称
      deptName: undefined, //部门
  };

  let form = reactive({ ...formDefault });

  const rules = {
      // deviceName: [{ required: true, message: '设备名称 必填' }],
      // ip: [{ required: false, message: 'ip 必填' }],
      // mask: [{ required: false, message: '掩码 必填' }],
      // leader: [{ required: false, message: '负责人 必填' }],
      // type: [{ required: false, message: '请选择设备类型' }],
      // hostName: [{ required: false, message: '安全域名称 必填' }],
      // deptName: [{ required: false, message: '部门 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      // 确保type是数字类型
      const formData = {
        ...form,
        type: form.type ? Number(form.type) : form.type
      };
      
      if (form.id) {
        await cmdbApi.update(formData);
      } else {
        await cmdbApi.add(formData);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
